//
//  SearchNavigationView.m
//  iMobile
//
//  Created by baken on 2016/12/7.
//  Copyright © 2016年 MyApp. All rights reserved.
//

#import "SearchNavigationView.h"

@interface SearchNavigationView()<UITextFieldDelegate>



@end

@implementation SearchNavigationView

- (instancetype)initWithFrame:(CGRect)frame{
    if ([super initWithFrame:frame]) {
        self.backgroundColor = UIColorFromRGB(kBackGroundColor);
        [self initNavigationBar];
    }
    return self;
}

- (void)initNavigationBar{
    
    _textfield = [[UITextField alloc] init];
    _textfield.delegate = self;
    _textfield.placeholder = @"输入关键词，找有券商品";
    _textfield.layer.masksToBounds = YES;
    _textfield.layer.cornerRadius = 10;
    _textfield.backgroundColor = [UIColor whiteColor];
    _textfield.clearButtonMode = UITextFieldViewModeWhileEditing;
    _textfield.returnKeyType = UIReturnKeySearch;
    _textfield.font = [UIFont systemFontOfSize:14];
    _textfield.textColor = UIColorFromRGB(kDarkFontColor);
    _textfield.tintColor = UIColorFromRGB(kBackGroundColor);
    [self addSubview:_textfield];
    [_textfield mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(12);
        make.centerY.equalTo(self);
        make.height.mas_equalTo(33);
    }];
    UIButton *rightButton = [[UIButton alloc] init];
    [rightButton setTitle:@"取消" forState:UIControlStateNormal];
    [rightButton setTitleColor:UIColorFromRGB(0x333333) forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(backButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:rightButton];
    
    [rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-12);
        make.left.mas_equalTo(self.textfield.mas_right).offset(10);
        make.centerY.equalTo(self);
        make.width.mas_equalTo(40);
    }];
    
    
    
    
    
    UIImageView *searchImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"search_left"]];
    _textfield.leftView = searchImage;
    _textfield.leftViewMode = UITextFieldViewModeAlways;
    
    
    UIView *lineView = [[UIView alloc] init];
    [self addSubview:lineView];
    lineView.backgroundColor = UIColorFromRGB(0xd4d4d4);
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(self.frame.size.width, 1));
        make.right.bottom.left.offset(0);
    }];
}

- (void)backButtonClick{
    [_delegate backButtonClick];
    
}

- (void)setSearchText:(NSString *)searchText{
    _searchText = searchText;
    self.textfield.text = searchText;
}



- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    /*
     每点击一次键盘执行一次
     当输入的时候textField.text是当前textField中已经存在的文字；
     range中的location表示当前已经输入的字符中准备change的哪个字符的range；range.location表示即将改变的字符在已经text中的位置；range.length表示要改变的text的长度；如果是删除，要删掉一个text的字符所以是1，新输入的字符不改变原text所以是0
     rang。localtion是当前已输入文字的长度；
     每输入一次string就是当前正在输入的文字
     textField.text +string就是当前textField中全部要输入的文字
     */
    
    //    [_delegate filterHistoryResult:textField.text andRange:range andReplaceString:string];
    return YES;
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    if ([_delegate respondsToSelector:@selector(searchButtonClick:)]) {
        [_delegate searchButtonClick:textField.text];
    }
    
    return YES;
}

- (BOOL)textFieldShouldClear:(UITextField *)textField{
    if ([_delegate respondsToSelector:@selector(clearButtonClick)]) {
        [_delegate clearButtonClick];
    }
    
    return YES;
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    if ([_delegate respondsToSelector:@selector(beginEdit)]) {
        [textField resignFirstResponder];
        [_delegate beginEdit];
    }
    if (self.isSearchPop) {
        return YES;
    } else {
        return NO;
    }
}



@end
